<?php

namespace app\aiv110\controller;

use app\aiv110\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\aiv110\model\Custom;
use app\aiv110\model\Miaosha;
use app\aiv110\model\Overgroup;
use app\aiv110\model\CtmOvergroup;
use app\aiv110\model\CtmMiaosha;
use app\aiv110\model\Order;
use app\aiv110\model\OrdPro;
use app\aiv110\model\OvergroupItem;
use app\aiv110\model\Redpacket;
use app\aiv110\model\StoreSoldoutitem;
use app\aiv110\model\CtmSpecial;

class OrderController extends CommonController {
    /*
     * code列表
     */

    public function code() {
        /*
         * 0  成功
         * 1  参数错误
         * 2  验证失败
         * 30000  每个超值组合每人限购一次
         * 30001  超值组合已下架
         * 30002  每个项目的限时抢购每人限购一次
         * 30003  限时抢购还没有开始
         * 30004  该项目已抢光，下次活动早点来哦~去看看其他项目吧~
         * 30005  该项目抢购活动已关闭，去看看其他项目吧~
         * 30006  请输入零钱支付密码
         * 30007  零钱支付密码错误
         * 30008  下单失败
         * 30009  请先付款
         * 30010  不满足返红包条件
         * 30011  不可以下单
         * 30012  代金券不可用
         * 30013
         * 30014
         * 30015
         * 30016
         */
    }

    /*
     * 生成订单
     * author xfwed 
     * date 2019.09.24
     * 
     * @param int perPage 每页数量 非必须，默认20
     * @param int page 当前页数 非必须，默认0
     */

    public function insertOrder() {
        $lockFile = ROOT_PATH . 'application/util/lock_miaosha.txt';
        $f = fopen($lockFile, 'r');
        if (flock($f, LOCK_EX)) {
            $rule = [
                'type' => 'require|in:2,5',
                'pro_id' => 'require|integer',
                'ctm_id' => 'require',
                'real_money' => 'require',
                'pay_money' => 'require',
                'balance_money' => 'require',
            ];
            $msg = [
                'type.require' => '类型必须',
                'type.in' => '类型为2/5',
                'pro_id.require' => '产品编号必须',
                'pro_id.integer' => '产品编号为数字',
                'ctm_id.require' => '用户id必须',
                'real_money.require' => '订单实际金额必须',
                'pay_money.require' => '订单实付金额必须',
                'balance_money.require' => '订单零钱必须',
            ];

            $all = input('param.');

            $validate = new Validate($rule, $msg);
            if (!$validate->check($all)) {
                $error = $validate->getError();

                return Utils::ajaxReturn(1, $error, null);
            }

            $type = $all['type'];
            $pro_id = $all['pro_id'];
            $ctm_id = $all['ctm_id'];
            $real_money = $all['real_money'];
            $pay_money = $all['pay_money'];
            $balance_money = $all['balance_money'];
            $vip_pwd = input('param.vip_pwd');
            $staff_id = input('param.staff_id');
            $staff_id = isset($staff_id)?$staff_id:0;
            $store_id = input('param.store_id');
            $cun_id = input('param.cun_id');//代金券id
            Db::startTrans();
            if(empty($cun_id)){
                $cun_id = 0;
                $ctm_cun_id = 0;
                $savectmCun = true;
            }  else {
                $ctmCun = Db::name('ctm_cun')
                        ->where('cun_id',$cun_id)
                        ->where('ctm_id',$ctm_id)
                        ->where('employ',2)
                        ->find();
                if(empty($ctmCun)){
                    return Utils::ajaxReturn(30012, '代金券不可用', null);
                }
                $savectmCun = Db::name('ctm_cun')
                        ->where('cun_id',$cun_id)
                        ->where('ctm_id',$ctm_id)
                        ->where('employ',2)
                        ->update(['employ'=>1]);
                $ctm_cun_id = $ctmCun['id'];
                        
            }
            if($pay_money <0){
                $pay_money = abs($pay_money)/10;
            }
            if($real_money <0){
                $real_money = abs($real_money)/10;
            }

            //用户信息
            $ctmRow = Custom::where('id', $ctm_id)->field('id,vip_pwd,real_money,red_money')->find();

            //产品信息
            if ($type == 2) {//超值组合
                $overGroupRow = Overgroup::where('id', $pro_id)->field('id,type,status,price')->find();
    //            $ctmOverGroupRow = CtmOvergroup::where('ctm_id', '=', $ctm_id)
    //                    ->where('overgroup_id', $pro_id)
    //                    ->find();
    //            if (!is_null($ctmOverGroupRow)) {
    //                return Utils::ajaxReturn(30000, '每个超值组合每人限购一次', null);
    //            }
                if ($overGroupRow['status'] == 2) {
                    return Utils::ajaxReturn(30001, '超值组合已下架', null);
                }
                $unservice_num = OvergroupItem::where('overgroup_id', '=', $pro_id)->count();
                $price = $overGroupRow['price'];
            } elseif ($type == 5) {
                $miaoshaRow = Miaosha::where('id', $pro_id)->field('item_id,start_time,number,sell_count,is_del,price')->find();
                $ctmMiaoshaRow = CtmMiaosha::where('ctm_id', $ctm_id)
                        ->where('item_id', '=', $miaoshaRow['item_id'])
                        ->find();
                if (!is_null($ctmMiaoshaRow)) {
                    return Utils::ajaxReturn(30002, '每个项目的限时抢购每人限购一次', null);
                }
                if ($miaoshaRow['start_time'] > date('Y-m-d H:i:s')) {
                    return Utils::ajaxReturn(30003, '限时抢购还没有开始', null);
                }
                if ($miaoshaRow['number'] - $miaoshaRow['sell_count'] <= 0) {
                    return Utils::ajaxReturn(30004, '该项目已抢光，下次活动早点来哦~去看看其他项目吧~', null);
                }
                if ($miaoshaRow['is_del'] == 2) {
                    return Utils::ajaxReturn(30005, '该项目抢购活动已关闭，去看看其他项目吧~', null);
                }
                $unservice_num = 1;
                $price = $miaoshaRow['price'];
            }
            if ($balance_money != 0) {
                if (empty($vip_pwd)) {
                    return Utils::ajaxReturn(30006, '请输入零钱支付密码', null);
                }
                if ($ctmRow['vip_pwd'] != strtoupper(md5($vip_pwd))) {
                    return Utils::ajaxReturn(30007, '零钱支付密码错误', null);
                }
                if($type == 2){
                    $red_money = 0;
                    if($ctmRow['real_money']<$balance_money){
                        return Utils::ajaxReturn(30007, '零钱有误', null);
                    }
                    $balance_money = $balance_money;
                } else {
                    $cred_money = $ctmRow['red_money'] - $balance_money;
                    if ($cred_money >= 0) {//红包够减
                        $red_money = $balance_money;
                        $balance_money = 0;
                    } elseif ($cred_money < 0) {
                        $red_money = $ctmRow['red_money'];
                        $balance_money = abs($cred_money);
                    }
                }
            } else {
                $red_money = 0;
                $balance_money = 0;
            }
            if ($pay_money != 0) {
                $ordtype = 1;
            } else {
                $ordtype = 2;
            }
            $ord_num = Utils::generateOrdNum($type);

            
            $orderData = [
                'source' => 1,
                'type' => $type,
                'staff_id' => $staff_id,
                'store_id' => $store_id,
                'ctm_id' => $ctm_id,
                'ordtype' => $ordtype,
                'real_money' => $real_money,
                'pay_money' => $pay_money,
                'balance_money' => $balance_money,
                'red_money' => $red_money,
                'ord_num' => $ord_num,
                'add_time' => date('Y-m-d H:i:s'),
                'cun_id' => $cun_id,
                'ctm_cun_id' => $ctm_cun_id,
            ];
            $orderId = Order::insertGetId($orderData);

            $ordProData = [
                'ord_id' => $orderId,
                'type' => $type,
                'pro_id' => $pro_id,
                'number' => 1,
                'unservice_num' => $unservice_num,
                'price' => $price,
                'add_time' => date('Y-m-d H:i:s'),
            ];
            $ordProId = OrdPro::insertGetId($ordProData);
            if($type == 5){
                $setincmiaosha = Miaosha::where('id', $pro_id)->setInc('sell_count',1);
            }  else {
                $setincmiaosha = TRUE;
            }

            if ($orderId && $ordProId && $setincmiaosha && $savectmCun) {
                Db::commit();
                if ($pay_money == 0) {
                    $ImgPath = config('appconfig.ApiPath');
                    Utils::curlPost1($ImgPath . 'order/dealOrderPro/' . $ord_num);
                }
                if ($type == 5) {
                    $TaskPath = config('appconfig.TaskPath');
                    $SelfPath = config('appconfig.SelfPath');
                    $postFields['source'] = 'kkmai'; //source String 是  来源（applicationName）
                    $postFields['taskType'] = 'SINGLE'; //taskType String 是   SINGLE(单次), MANY（多次）
                    $postFields['methodName'] = $SelfPath . 'Order/cancelOrder/id/' . $orderId; //methodName String 是  需要执行的方法名
                    $postFields['methodType'] = 'POST'; //methodType String 是  需要执行的方法调用方式（ GET,POST）
                    //$postFields['methodParams'] = 'kkmai'; //methodParams Object 否  需要执行的方法参数
                    $postFields['executeTime'] = date('Y-m-d H:i:s', time() + 3600 * 20); //executeTime String 是 19 第一次执行时间（2019-06-01 13:12:14）
                    //$postFields['intervalTime']=3600;//intervalTime Integer 否  间隔时间（若taskType=MANY，填写）秒
                    Utils::curlPost($TaskPath . 'task/addTask/', json_encode($postFields));
                }
                $data['ord_num'] = $ord_num;
                $data['orderId'] = $orderId;
                $data['ordtype'] = $ordtype; //支付方式 1小程序 2余额
                return Utils::ajaxReturn(0, '下单成功', $data);
            } else {
                Db::rollback();
                return Utils::ajaxReturn(30008, '下单失败', null);
            }
        }
        fclose($f);
    }

    /*
     * 取消订单
     * author xfwed 
     * date 2019.09.25
     * 
     * 调度任务接口回调
     */

    public function cancelOrder() {
        $id = input('param.id');
        $orderRow = Order::where('id', $id)->field('type,status')->find();
        if ($orderRow['status'] == 1) {
            Db::startTrans();
            $row = Order::where('id', $id)->update(['status' => 0, 'cancel_time' => date('Y-m-d H:i:s')]);
            if($orderRow['type'] == 5){
                $pro_id = OrdPro::where('ord_id',$id)->value('pro_id');
                $setincmiaosha = Miaosha::where('id', $pro_id)->setDec('sell_count',1);
            }  else {
                $setincmiaosha = TRUE;
            }
            
            if ($row && $setincmiaosha) {
                Db::commit();
                return Utils::ajaxReturn(0, '执行成功', null);
            }  else {
                 Db::rollback();
                 return Utils::ajaxReturn(3, '执行失败', null);
            }
        }  else {
            return Utils::ajaxReturn(0, '执行成功', null);
        }
    }

    public function test() {
        //$ImgPath = config('appconfig.ApiPath');
        //return Utils::curlPost($ImgPath . 'order/dealOrderPro/X15693766193985809');

        $TaskPath = config('appconfig.TaskPath');
        $SelfPath = config('appconfig.SelfPath');
        $postFields['source'] = 'kkmai'; //source String 是  来源（applicationName）
        $postFields['taskType'] = 'SINGLE'; //taskType String 是   SINGLE(单次), MANY（多次）
        $postFields['methodName'] = $SelfPath . 'Order/cancelOrder/id/' . '25'; //methodName String 是  需要执行的方法名
        $postFields['methodType'] = 'POST'; //methodType String 是  需要执行的方法调用方式（ GET,POST）
        //$postFields['methodParams'] = 'kkmai'; //methodParams Object 否  需要执行的方法参数
        $postFields['executeTime'] = date('Y-m-d H:i:s', time() + 60); //executeTime String 是 19 第一次执行时间（2019-06-01 13:12:14）
        //$postFields['intervalTime']=3600;//intervalTime Integer 否  间隔时间（若taskType=MANY，填写）秒

        return Utils::curlPost($TaskPath . 'task/addTask/', json_encode($postFields));
    }
    
    
    /*
     * 下单返红包（检测）
     * author xfwed 
     * date 2019.11.19
     * 
     * @param string ordNum 订单编号 必须，无默认
     */

    public function orderRedPacket() {
        $rule = [
            'ordNum' => 'require',
        ];
        $msg = [
            'ordNum.require' => '订单编号必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ordNum = $all['ordNum'];

        //订单信息
        $orderdata = Order::where('ord_num',$ordNum)->field('id,type,ctm_id,back_redmoney,pay_time')->find();
        $type = $orderdata['type'];
        if($type == 1){
            $is_imeigroup = OrdPro::alias('o')
                ->join('item i','i.id = o.pro_id','left')
                ->where('o.ord_id',$orderdata['id'])
                ->value('is_imeigroup');
        }  else {
            $is_imeigroup = 1;
        }
                
        //i没套餐不返红包
        if($type == 1 && $is_imeigroup == 2){
            $vip_status = Custom::where('id',$orderdata['ctm_id'])->value('vip_status');
            $ctmSpecial = CtmSpecial::where('ctm_id',$orderdata['ctm_id'])
                    ->whereTime('end_time','>',date('Y-m-d H:i:s'))
                    ->order('end_time asc')
                    ->find();
            if($ctmSpecial){
                $ctmSpecialTime = $ctmSpecial['end_time'];
            }  else {
                $ctmSpecialTime = '2082-07-06 00:00:00';//随便写了到期时间
            }
            $ctmSpecialTime = strtotime($ctmSpecialTime); 
            $c = $ctmSpecialTime - 31536000;//减去一年的时间 算出购买特权的时间
            $ctmSpecialTime = date('Y-m-d H:i:s',$c);
            if(($vip_status == 2 || $vip_status == 3) && $orderdata['pay_time'] > '2020-08-27 00:00:00'){
                if($orderdata['pay_time'] > $ctmSpecialTime){
                    $tan = 1;
                }  else {
                    $tan = 2;
                }
                
            }  else {
                $tan = 2;
            }
        }  else {
            $tan = 2;
        }
        $back_redmoney = $orderdata['back_redmoney'];
        if($back_redmoney != 0){
            $tan = 2;
        }
        Order::where('ord_num',$ordNum)->update(['is_tan'=>$tan]);
        return Utils::ajaxReturn(0, '查询成功', $tan);
    }
    
    /*
     * 下单返红包（领红包）
     * author xfwed 
     * date 2019.11.19
     * 
     * @param string ordNum 订单编号 必须，无默认
     */

    public function orderGetRedPacket() {
        $rule = [
            'ordNum' => 'require',
        ];
        $msg = [
            'ordNum.require' => '订单编号必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $ordNum = $all['ordNum'];

        //订单信息
        $orderdata = Order::where('ord_num',$ordNum)->field('type,status,ctm_id,back_redmoney')->find();
        $type = $orderdata['type'];
        $status = $orderdata['status'];
        $ctm_id = $orderdata['ctm_id'];
        $back_redmoney = $orderdata['back_redmoney'];
        if($status != 2){
            return Utils::ajaxReturn(30009, '请先付款', null);
        }
        if($back_redmoney != 0){
            return Utils::ajaxReturn(30009, '红包已经领取', null);
        }
        if($type == 1 || $type == 2){
            $vip_status = Custom::where('id',$orderdata['ctm_id'])->value('vip_status');
            if($vip_status == 2 || $vip_status == 3){
                //每个用户领取5次红包后  领取金额变成0.01-10
//                $num = Order::where('ctm_id',$orderdata['ctm_id'])->where('back_redmoney','<>',0)->count();
//                if($num >= 5){
//                    $red_money = round(10+mt_rand()/mt_getrandmax()*(30-10), 2);
//                }  else {
//                    $red_money = round(10+mt_rand()/mt_getrandmax()*(30-10), 2);
//                }
                //1-10 45%   10-15  35% | 15-20  10% | 20-25  5% | 25-30  5%
                $rand=rand(1,100);
                if($rand >=1 && $rand <=45){
                    $red_money = round(1+mt_rand()/mt_getrandmax()*(10-1), 2);
                }elseif ($rand >=46 && $rand <=80) {
                    $red_money = round(10+mt_rand()/mt_getrandmax()*(15-10), 2);
                }elseif ($rand >=81 && $rand <=90) {
                    $red_money = round(15+mt_rand()/mt_getrandmax()*(20-15), 2);
                }elseif ($rand >=91 && $rand <=95) {
                    $red_money = round(20+mt_rand()/mt_getrandmax()*(25-20), 2);
                }elseif ($rand >=96 && $rand <=100) {
                    $red_money = round(25+mt_rand()/mt_getrandmax()*(30-25), 2);
                }
                
                $shareResult = Custom::where('id', '=', $ctm_id)->setInc('red_money', $red_money*100);
                $redData['ctm_id'] = $ctm_id;
                $redData['money'] = $red_money*100;
                $redData['type'] = 2;
                $redData['add_time'] = date('Y-m-d H:i:s',  time());
                Redpacket::insert($redData);
                Order::where('ord_num',$ordNum)->update(['back_redmoney'=>$red_money*100]);
                return Utils::ajaxReturn(0, '领取成功', $red_money);
            }  else {
                return Utils::ajaxReturn(30010, '不满足返红包条件', null);
            }
        }  else {
            return Utils::ajaxReturn(30010, '不满足返红包条件', null);
        }
        
    }
    /*
     * 下单检测店铺是否可以服务该项目
     * author xfwed 
     * date 2020.05.09
     */

    public function storeItem() {
        $rule = [
            'store_id' => 'require',
            //'item_id' => 'require',
        ];
        $msg = [
            'store_id.require' => '店铺id必须',
            //'item_id.require' => '项目id必须',
        ];

        $all = input('param.');

        $validate = new Validate($rule, $msg);
        if (!$validate->check($all)) {
            $error = $validate->getError();

            return Utils::ajaxReturn(1, $error, null);
        }

        $store_id = $all['store_id'];
        $item_id = input('param.item_id');
        $type = input('param.type');//1项目2超值组合3秒杀
        $type = isset($type)?$type:1;
       
//        $store_ids = array("5","6","7","8","10","11","13");
//        $item_ids = array("11","13","16","19","20","28","29","18","21");
//        
//        if($item_id){
//            $item_id = explode(',',$item_id);
//            $new = array_intersect($item_id,$item_ids);
//
//            if ($new) {
//                if(in_array($store_id, $store_ids)){
//                    return Utils::ajaxReturn(0, '可以下单', null);
//                }  else {
//                    return Utils::ajaxReturn(30011, '此订单中包含仅能在北京朝外SOHO店、国美店；安徽合肥蒙城北路店；山西运城条山街店、河北廊坊万达店、新开路店；山东烟台观海路店服务的项目，请重新选择服务项目或门店', null);
//                }
//            } else {
//                return Utils::ajaxReturn(0, '可以下单', null);
//            }
//        } else {
//            return Utils::ajaxReturn(0, '可以下单', null);
//        }
        

        
//        $list = [
//            ['store_id'=>1,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            //['store_id'=>5,'item_id'=>["11","13","16","19","20","28","29"]],//店铺不买的项目
//            //['store_id'=>6,'item_id'=>["11","13","16","19","20","28","29"]],//店铺不买的项目
//            //['store_id'=>7,'item_id'=>["11","13","16","19","20","28","29"]],//店铺不买的项目
//            //['store_id'=>8,'item_id'=>["11","13","16","19","20","28","29"]],//店铺不买的项目
//            ['store_id'=>9,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            //['store_id'=>10,'item_id'=>["11","13","16","19","20","28","29"]],//店铺不买的项目
//            //['store_id'=>11,'item_id'=>["11","13","16","19","20","28","29"]],//店铺不买的项目
//            ['store_id'=>12,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            //['store_id'=>13,'item_id'=>["11","13","16","19","20","28","29"]],//店铺不买的项目
//            ['store_id'=>14,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>15,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>16,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>17,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>18,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>19,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>20,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>21,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>22,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>23,'item_id'=>["11","13","16","19","20","28","29","18","21","35","36","37","38"]],//店铺不买的项目
//            ['store_id'=>24,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>25,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>26,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>27,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>28,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>29,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>30,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>31,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>32,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>33,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>34,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>35,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>36,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>37,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>38,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>39,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>40,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>41,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>42,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>43,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>44,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//            ['store_id'=>45,'item_id'=>["11","13","16","19","20","28","29","18","21"]],//店铺不买的项目
//        ];
//        
//        if($item_id){
//            $item_id = explode(',',$item_id);
//            foreach ($list as $k => $v) {
//                if ($v['store_id'] == $store_id) {
//                    $new = array_intersect($item_id, $v['item_id']);
//                    if ($new) {
//                        return Utils::ajaxReturn(30011, '本店此项目已经售空', null);
//                    } else {
//                        return Utils::ajaxReturn(0, '可以下单', null);
//                    }
//                }
//            }
//            return Utils::ajaxReturn(0, '可以下单', null);
//        }else {
//            return Utils::ajaxReturn(0, '可以下单', null);
//        }
        if($type == 1){
            $item_ids = StoreSoldoutitem::where('store_id',$store_id)->value('item_ids');
            if($item_ids){
                $item_ids = explode(',',$item_ids);
                if($item_id){
                    $item_id = explode(',',$item_id);
                        $new = array_intersect($item_id, $item_ids);
                        if ($new) {
                            return Utils::ajaxReturn(30011, '该项目本店暂不可售 敬请期待', null);
                        } else {
                            return Utils::ajaxReturn(0, '可以下单', null);
                        }
                    return Utils::ajaxReturn(0, '可以下单', null);
                }else {
                    return Utils::ajaxReturn(0, '可以下单', null);
                }
            }  else {
                return Utils::ajaxReturn(0, '可以下单', null);
            }
        }elseif ($type == 2) {
            $overgroup_ids = StoreSoldoutitem::where('store_id',$store_id)->value('overgroup_ids');
            if($overgroup_ids){
                $overgroup_ids = explode(',',$overgroup_ids);
                if($item_id){
                    $item_id = explode(',',$item_id);
                        $new = array_intersect($item_id, $overgroup_ids);
                        if ($new) {
                            return Utils::ajaxReturn(30011, '该项目本店暂不可售 敬请期待', null);
                        } else {
                            return Utils::ajaxReturn(0, '可以下单', null);
                        }
                    return Utils::ajaxReturn(0, '可以下单', null);
                }else {
                    return Utils::ajaxReturn(0, '可以下单', null);
                }
            }  else {
                return Utils::ajaxReturn(0, '可以下单', null);
            }
        }elseif ($type == 3) {
            $miaosha_ids = StoreSoldoutitem::where('store_id',$store_id)->value('miaosha_ids');
            if($miaosha_ids){
                $miaosha_ids = explode(',',$miaosha_ids);
                if($item_id){
                    $item_id = explode(',',$item_id);
                        $new = array_intersect($item_id, $miaosha_ids);
                        if ($new) {
                            return Utils::ajaxReturn(30011, '该项目本店暂不可售 敬请期待', null);
                        } else {
                            return Utils::ajaxReturn(0, '可以下单', null);
                        }
                    return Utils::ajaxReturn(0, '可以下单', null);
                }else {
                    return Utils::ajaxReturn(0, '可以下单', null);
                }
            }  else {
                return Utils::ajaxReturn(0, '可以下单', null);
            }
        }
        
    }
}
